const watermark = {}

const setWatermark = (str) => {
  const id = '1.23452384164.123412416'
  if (str) {
    const can = document.createElement('canvas')
    can.width = 240
    can.height = 200

    const cans = can.getContext('2d')
    cans.rotate(-15 * Math.PI / 180)
    cans.font = '14px Vedana'
    cans.fillStyle = 'rgba(200, 200, 200, 0.40)'
    cans.textAlign = 'left'
    cans.textBaseline = 'Middle'
    cans.fillText(str, can.width / 8, can.height / 2)

    const div = document.createElement('div')
    div.id = id
    div.style.pointerEvents = 'none'
    div.style.top = '150px'
    div.style.left = '0px'
    div.style.position = 'fixed'
    div.style.zIndex = '0'
    div.style.width = document.body.clientWidth - 50 + 'px'
    div.style.height = document.body.clientHeight - 200 + 'px'
    div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'
    if (document.querySelector('.minder-base')) {
      if (document.getElementById(id)) {
        document.querySelector('.minder-base').removeChild(document.getElementById(id))
      }
      document.querySelector('.minder-base').appendChild(div)
    }
    return id
  }
  if (document.getElementById(id) !== null) {
    if (document.querySelector('.minder-base')) {
      document.querySelector('.minder-base').removeChild(document.getElementById(id))
    }
  }
}

watermark.set = (str) => {
  setWatermark(str)
  // setInterval(() => {
  //   if (document.getElementById(id) === null) {
  //     id = setWatermark(str)
  //   }
  // }, 500)
}

export default watermark
